<?php

/* 访问控制 */
defined('IN_ECTOUCH') or die('Deny Access');

class StoreController extends CommonController {

    protected $store_id;

    /**
     * 构造函数   加载user.php的语言包 并映射到模版
     */
    public function __construct() {
        parent::__construct();
        $this->store_id = isset($_REQUEST ['id']) ? intval($_REQUEST ['id']) : 0;
    }

    public function index() {

        $store = model('Store')->get_store_info($this->store_id);
        // 如果没有找到任何记录则跳回到首页
        if ($store === false) {
            ecs_header("Location: ./\n");
        } else {
            
            $this->assign('store', $store);
            $this->assign('store_id', $store ['store_id']);
        }

        $this->display('store.dwt');
    }

	public function show_map() {

        $store = model('Store')->get_store_info($this->store_id);
		$store_json[] = $store;
        // 如果没有找到任何记录则跳回到首页
        if ($store === false) {
            ecs_header("Location: ./\n");
        } else {
            
            $this->assign('store', $store);
            $this->assign('store_id', $store ['store_id']);
			$this->assign('store_json', json_encode($store_json));
        }

        $this->display('store_map.dwt');
    }

	public function storelist() {

        $this->display('storelist.dwt');
    }

	/**
     * ajax获取当前位置  附近的店铺
     */
    public function ajax_location() {
        if (IS_AJAX) {
            $lng = I('get.lng');
			$lng = $lng ? (round($lng,6)+0.0104) : '108.341952'; 
			$lat = I('get.lat');
			$lat = $lat ? (round($lat,6)+0.0025) : '22.821867';
			
            $distance = 20; $start = $_POST['last']; $limit = $_POST['amount'];
			
			$this->assign( 'rg_local', array('lat'=>$lat, 'lng'=>$lng) );

            $store_list = model('Store')->store_list($lng, $lat, $distance, $limit, $start);
            $list = array();
            if ($store_list) {
                foreach ($store_list as $key => $value) {
                    $this->assign('store_list', $value);
                    $list [] = array(
                        'single_item' => ECTouch::view()->fetch('library/asynclist_store.lbi')
                    );
                }
            }
            echo json_encode($list);
            exit();
        } else {
            $this->redirect(url('index'));
        }
    }

}